package com.lry.config;

import com.lry.bean.ExceptionNotice;
import com.lry.event.AsyncNoticeSendListener;
import com.lry.event.NoticeSendListener;
import com.lry.properties.EaNoticeProperties;
import com.lry.send.INoticeSendComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.Executor;

@Configuration
@EnableConfigurationProperties(EaNoticeProperties.class)
@ConditionalOnBean(INoticeSendComponent.class)
public class EaNoticeConfig {

    @Autowired
    private List<INoticeSendComponent<ExceptionNotice>> list;

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(value = "ea.notice.async",havingValue = "false")
    public NoticeSendListener noticeSendListener(){
        return new NoticeSendListener(list);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(value = "ea.notice.async",havingValue = "true",matchIfMissing = true)
    public AsyncNoticeSendListener asyncNoticeSendListener(Executor eaTaskExecutor){
        return new AsyncNoticeSendListener(list,eaTaskExecutor);
    }
}
